package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class MyTask {
    @Autowired
   private OrderMapper orderMapper;

    @Scheduled(cron = "0 * * * * ?")
 /*   @Scheduled(cron = "0/5 * * * * ?")*/

    public void executeEveryMinute(){
        log.info("正在处理订单:{}",LocalDateTime.now());
        LocalDateTime time = LocalDateTime.now().plusMinutes(-15);
      List<Orders> orders = orderMapper.executeEveryMinute(Orders.PENDING_PAYMENT,time);
       if (orders!=null && orders.size()>0){
        for (Orders order : orders) {
            order.setStatus(Orders.CANCELLED);
            order.setCancelTime(LocalDateTime.now());
            order.setCancelReason("支付超时");
            orderMapper.update(order);



        }
        }

    }
    @Scheduled(cron = "* * 1 * * ?")
    public void executeEveryDay() {
        LocalDateTime time = LocalDateTime.now().plusMinutes(-60);
        List<Orders> orders = orderMapper.executeEveryMinute(Orders.DELIVERY_IN_PROGRESS, time);
        if (orders != null && orders.size() > 0) {
            for (Orders order : orders) {
                order.setStatus(Orders.COMPLETED);
                orderMapper.update(order);


            }
        }
    }
}